package jp.bsws.action.syukei;

/*
 * ログインサンプル
 */
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpSession;

import jp.bsws.bean.KeyValueBean;
import jp.bsws.bean.MasterBean;
import jp.bsws.bean.GekkeiJohoBean;
import jp.bsws.bl.BSWSBl;
import jp.bsws.common.BSWSConstants;
import jp.bsws.common.BSWSSession;
import jp.bsws.common.BSWSUtils;
import jp.bsws.common.DBUtils;
import jp.bsws.dao.HosyuShubetsuDao;
import jp.bsws.dao.JugyoinDao;
import jp.bsws.dao.RaitenYoinDao;
import jp.bsws.dao.ShiharaiHohoDao;
import jp.bsws.dao.UriageShubetsuDao;
import jp.bsws.model.HosyuShubetsuModel;
import jp.bsws.model.RaitenYoinModel;
import jp.bsws.model.ShiharaiHohoModel;
import jp.bsws.model.UriageShubetsuModel;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
/*
 * メインメニューアクションクラス
 */
public class  GekkeiHyoAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private BSWSSession bSWSSession;

	private String strSansyoTenpoCod;
	private Integer intSansyoJugyoinSeq;
	private String strSansyoNengetsu;

	// ログインユーザ
	private List<KeyValueBean> kvTenpoList;
	private List<KeyValueBean> kvJugyoinList;
	private List<KeyValueBean> kvNengetsuFromList;
	private List<KeyValueBean> kvNengetsuToList;

	private List<GekkeiJohoBean> gekkeiHyoList;
	private List<MasterBean> raitenYoinHList;
	private List<MasterBean> uriageShubetsuHList;
	private List<MasterBean> shiharaiHohoHList;
	private List<MasterBean> hosyuShubetsuHList;
	private List<GekkeiJohoBean> gekkeiHyoGokeiList;
	private List<GekkeiJohoBean> gekkeiHyoAveList;
	private List<GekkeiJohoBean> gekkeiHyoYosokuList;

	// ■ログインチェック
	public String execute() throws Exception{

		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

		Connection con = DBUtils.getConnection();
		try{

			//参照可能店舗リスト
			this.kvTenpoList = bSWSSession.getSansyoTenpoList();
			//参照店舗がnullの場合、セッションから店舗を取得
			if(BSWSUtils.isBlankNull(strSansyoTenpoCod)){
				strSansyoTenpoCod = bSWSSession.getStrTenpoCod();
			}else{
				bSWSSession.setStrTenpoCod(strSansyoTenpoCod) ;
			}

			//参照可能従業員リスト
			this.kvJugyoinList = new ArrayList<KeyValueBean>();
			if(BSWSConstants.C_JUGYOIN_SANSYO_ALL.equals(bSWSSession.getStrJugyoinSansyoKbn())){
				JugyoinDao jugyoinDao = JugyoinDao.getInstance();
				this.kvJugyoinList =  jugyoinDao.getJugyoinList(con, bSWSSession.getStrGroupCod(), strSansyoTenpoCod );
			}else{
				this.kvJugyoinList.add(new KeyValueBean(bSWSSession.getStrJugyoinCod(),bSWSSession.getStrJugyoinKnj()));
			}
			//参照可能年月リストFROM
			this.kvNengetsuFromList = BSWSUtils.getSansyoNengetsuList();
			//参照可能年月リストTO
//			this.kvNengetsuToList = BSWSUtils.getSansyoNengetsuList();

			//ヘッダー情報取得
			RaitenYoinDao raitenYoinDao = RaitenYoinDao.getInstance();
//			UriageShubetsuDao uriageShubetsuDao = UriageShubetsuDao.getInstance();
			ShiharaiHohoDao shiharaiHohoDao = ShiharaiHohoDao.getInstance();
//			HosyuShubetsuDao hosyuShubetsuDao = HosyuShubetsuDao.getInstance();

			//来店要因情報取得
			List<RaitenYoinModel> raitenYoinList= raitenYoinDao.getRaitenYoinJoho(con, null, bSWSSession.getStrGroupCod(), strSansyoTenpoCod);
//			//売上種別情報取得
//			List<UriageShubetsuModel> uriageShubetsuList= uriageShubetsuDao.getUriageShubetsuJoho(con, null, bSWSSession.getStrGroupCod(), strSansyoTenpoCod);
			//支払方法情報取得
			List<ShiharaiHohoModel> shiharaiHohoList= shiharaiHohoDao.getShiharaiHohoJoho(con, null, bSWSSession.getStrGroupCod(), strSansyoTenpoCod);
//			//報酬種別情報取得
//			List<HosyuShubetsuModel> HosyuShubetsuList= hosyuShubetsuDao.getHosyuShubetsuJoho(con, null, bSWSSession.getStrGroupCod(), strSansyoTenpoCod);

			raitenYoinHList = new ArrayList<MasterBean>();
//			uriageShubetsuHList = new ArrayList<MasterBean>();
			shiharaiHohoHList = new ArrayList<MasterBean>();
//			hosyuShubetsuHList = new ArrayList<MasterBean>();
			// ヘッダーの取得
			for(RaitenYoinModel raitenYoinModel : raitenYoinList) {
				if(BSWSConstants.C_HYOJI.equals(raitenYoinModel.getStrRaitenYoinHyojiFlg())){
					MasterBean masterBean = new MasterBean();
					masterBean.setIntSeq(raitenYoinModel.getSeq());
					masterBean.setStrValue(raitenYoinModel.getStrRaitenYoinRyakuKnj());
					raitenYoinHList.add(masterBean);
				}
			}
//			for(UriageShubetsuModel uriageShubetsuModel : uriageShubetsuList) {
//				if(BSWSConstants.C_HYOJI.equals(uriageShubetsuModel.getStrUriageShubetsuHyojiFlg())){
//					MasterBean masterBean = new MasterBean();
//					masterBean.setIntSeq(uriageShubetsuModel.getSeq());
//					masterBean.setStrValue(uriageShubetsuModel.getStrUriageShubetsuRyakuKnj());
//					uriageShubetsuHList.add(masterBean);
//				}
//			}
			for(ShiharaiHohoModel shiharaiHohoModel : shiharaiHohoList) {
				if(BSWSConstants.C_HYOJI.equals(shiharaiHohoModel.getStrShiharaiHohoHyojiFlg())){
					MasterBean masterBean = new MasterBean();
					masterBean.setIntSeq(shiharaiHohoModel.getSeq());
					masterBean.setStrValue(shiharaiHohoModel.getStrShiharaiHohoRyakuKnj());
					shiharaiHohoHList.add(masterBean);
				}
			}
//			for(HosyuShubetsuModel hosyuShubetsuModel : HosyuShubetsuList) {
//				if(BSWSConstants.C_HYOJI.equals(hosyuShubetsuModel.getStrHosyuShubetsuHyojiFlg())){
//					MasterBean masterBean = new MasterBean();
//					masterBean.setIntSeq(hosyuShubetsuModel.getSeq());
//					masterBean.setStrValue(hosyuShubetsuModel.getStrHosyuShubetsuRyakuKnj());
//					hosyuShubetsuHList.add(masterBean);
//				}
//			}

			BSWSBl bSWSBl = new BSWSBl();

			//日付の加工
			String tmpFromYmd = "";
			String tmpToYmd = "";
			//どちらも空（初期表示）の場合、システム日付の年月で検索
			if(BSWSUtils.isBlankNull(strSansyoNengetsu)){
				tmpFromYmd = BSWSUtils.getSysDateYmdAtGessyo();
				tmpToYmd = BSWSUtils.getSysDateYmdAtMatsujitsu();
				strSansyoNengetsu = BSWSUtils.getSysDateYm();
			}

			if(BSWSUtils.isNotBlankNull(strSansyoNengetsu) && strSansyoNengetsu.length() == 6){
				tmpFromYmd = strSansyoNengetsu + "01";
				Calendar cal = Calendar.getInstance();
				String tmpYYYYMM = tmpFromYmd.substring(0, 6);
				int iyy = Integer.parseInt(tmpFromYmd.substring(0, 4));
				int imm = Integer.parseInt(tmpFromYmd.substring(4, 6));
				cal.set(Calendar.YEAR, iyy);
				cal.set(Calendar.MONTH, imm - 1);
				int idd = cal.getActualMaximum(Calendar.DATE);
				String dd = BSWSUtils.hidariUme(String.valueOf(idd),2,"0");
				tmpToYmd = tmpYYYYMM + dd;
			}

			//集計情報取得
			gekkeiHyoList = bSWSBl.getGekkeiJoho(con, bSWSSession,
					raitenYoinHList,uriageShubetsuHList,shiharaiHohoHList,hosyuShubetsuHList,
					strSansyoTenpoCod, intSansyoJugyoinSeq, tmpFromYmd, tmpToYmd);

			GekkeiJohoBean tmpGokei = bSWSBl.getGokeiGekkeiBean(gekkeiHyoList, tmpFromYmd, tmpToYmd);
			gekkeiHyoGokeiList = new ArrayList<GekkeiJohoBean>();
			gekkeiHyoAveList = new ArrayList<GekkeiJohoBean>();
			gekkeiHyoYosokuList = new ArrayList<GekkeiJohoBean>();
			//合計値取得
			gekkeiHyoGokeiList.add(tmpGokei);
			//平均値取得（画面で計算）
			gekkeiHyoAveList.add(tmpGokei);
			//予測値取得（画面で計算）
			gekkeiHyoYosokuList.add(tmpGokei);

		}catch(SQLException e){
			con.rollback();
			throw e;
		}finally{
			con.close();
		}
		return SUCCESS;
	}

	public BSWSSession getBSWSSession() {
		return bSWSSession;
	}

	public void setBSWSSession(BSWSSession bSWSSession) {
		this.bSWSSession = bSWSSession;
	}

	public String getStrSansyoTenpoCod() {
		return strSansyoTenpoCod;
	}

	public void setStrSansyoTenpoCod(String strSansyoTenpoCod) {
		this.strSansyoTenpoCod = strSansyoTenpoCod;
	}

	public Integer getIntSansyoJugyoinSeq() {
		return intSansyoJugyoinSeq;
	}

	public void setIntSansyoJugyoinSeq(Integer intSansyoJugyoinSeq) {
		this.intSansyoJugyoinSeq = intSansyoJugyoinSeq;
	}

	public List<KeyValueBean> getKvTenpoList() {
		return kvTenpoList;
	}

	public void setKvTenpoList(List<KeyValueBean> kvTenpoList) {
		this.kvTenpoList = kvTenpoList;
	}

	public List<KeyValueBean> getKvJugyoinList() {
		return kvJugyoinList;
	}

	public void setKvJugyoinList(List<KeyValueBean> kvJugyoinList) {
		this.kvJugyoinList = kvJugyoinList;
	}

	public List<KeyValueBean> getKvNengetsuFromList() {
		return kvNengetsuFromList;
	}

	public void setKvNengetsuFromList(List<KeyValueBean> kvNengetsuFromList) {
		this.kvNengetsuFromList = kvNengetsuFromList;
	}

	public List<KeyValueBean> getKvNengetsuToList() {
		return kvNengetsuToList;
	}

	public void setKvNengetsuToList(List<KeyValueBean> kvNengetsuToList) {
		this.kvNengetsuToList = kvNengetsuToList;
	}

	public List<GekkeiJohoBean> getGekkeiHyoList() {
		return gekkeiHyoList;
	}

	public void setGekkeiHyoList(List<GekkeiJohoBean> gekkeiHyoList) {
		this.gekkeiHyoList = gekkeiHyoList;
	}

	public List<MasterBean> getRaitenYoinHList() {
		return raitenYoinHList;
	}

	public void setRaitenYoinHList(List<MasterBean> raitenYoinHList) {
		this.raitenYoinHList = raitenYoinHList;
	}

	public List<MasterBean> getUriageShubetsuHList() {
		return uriageShubetsuHList;
	}

	public void setUriageShubetsuHList(List<MasterBean> uriageShubetsuHList) {
		this.uriageShubetsuHList = uriageShubetsuHList;
	}

	public List<MasterBean> getShiharaiHohoHList() {
		return shiharaiHohoHList;
	}

	public void setShiharaiHohoHList(List<MasterBean> shiharaiHohoHList) {
		this.shiharaiHohoHList = shiharaiHohoHList;
	}

	public List<MasterBean> getHosyuShubetsuHList() {
		return hosyuShubetsuHList;
	}

	public void setHosyuShubetsuHList(List<MasterBean> hosyuShubetsuHList) {
		this.hosyuShubetsuHList = hosyuShubetsuHList;
	}

	public List<GekkeiJohoBean> getGekkeiHyoGokeiList() {
		return gekkeiHyoGokeiList;
	}

	public void setGekkeiHyoGokeiList(List<GekkeiJohoBean> gekkeiHyoGokeiList) {
		this.gekkeiHyoGokeiList = gekkeiHyoGokeiList;
	}

	public List<GekkeiJohoBean> getGekkeiHyoAveList() {
		return gekkeiHyoAveList;
	}

	public void setGekkeiHyoAveList(List<GekkeiJohoBean> gekkeiHyoAveList) {
		this.gekkeiHyoAveList = gekkeiHyoAveList;
	}

	public List<GekkeiJohoBean> getGekkeiHyoYosokuList() {
		return gekkeiHyoYosokuList;
	}

	public void setGekkeiHyoYosokuList(List<GekkeiJohoBean> gekkeiHyoYosokuList) {
		this.gekkeiHyoYosokuList = gekkeiHyoYosokuList;
	}

	public String getStrSansyoNengetsu() {
		return strSansyoNengetsu;
	}

	public void setStrSansyoNengetsu(String strSansyoNengetsu) {
		this.strSansyoNengetsu = strSansyoNengetsu;
	}
}